using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using NCS.Module.CMS.Library.Distributions;
using NCS.Module.CMS.Library.Definitions;
using NCS.Module.CMS.Library.Zones;
using NCS.Library.Utilities;
using NCS.Module.CMS.Library;

namespace NCS.Module.CMS.Web.HomePage
{
    public partial class HeadZone : CMSWebControl
    {
        protected string zoneurl = "";
        private int _zoneid = 0;
        private ZoneInfo zoneinfo = null;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void LoadZone(int zoneid)
        {
            zoneinfo = ZoneController.GetZone(zoneid);
            if (zoneinfo != null)
            {
                this._zoneid = zoneid;
                this.lab_ZoneTitle.Text = zoneinfo.Zone_Name;

                this.zoneurl = BuildUrlByZone(this.zoneinfo.Zone_ID, this.zoneinfo.Portal_TabControlID);

                DataTable tbl = DistributionController.GetDistributedContents_ByZone(zoneid, 4);//DistributionController.GetDistributedContents(zoneid, 4, (int)CMSDistributionRank.Default);
                if (tbl != null && tbl.Rows.Count > 0)
                {
                    DataTable tbl1 = tbl.Clone();
                    tbl1.ImportRow(tbl.Rows[0]);

                    this.rpt_head.DataSource = tbl1;
                    this.rpt_head.DataBind();

                    DataTable tbl2 = tbl.Clone();
                    for (int i = 1; i < tbl.Rows.Count; i++)
                        tbl2.ImportRow(tbl.Rows[i]);

                    this.rpt_foot.DataSource = tbl2;
                    this.rpt_foot.DataBind();
                }
                else
                {
                    this.Visible = false;
                }
            }
            else
            {
                this.Visible = false;
            }
        }

        protected void rpt_foot_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                DataRowView currow = (DataRowView)e.Item.DataItem;
                if (currow != null)
                {
                    HyperLink hplTitle = (HyperLink)e.Item.FindControl("link");
                 

                    //string tooltip = MiscUtility.ToolTip(ConvertUtility.ToString(currow["Content_Headline"]), ConvertUtility.ToString(currow["Content_SubTitle"]), ConvertUtility.ToString(currow["Content_Avatar"]), ConvertUtility.ToString(currow["Content_Teaser"]), ConvertUtility.ToString(currow["Content_Alias"]), ConvertUtility.ToDateTime(currow["Distribution_CreateDate"]));
                    //hplTitle.Attributes.Add("onmouseover", "setVisibility(event,'" + currow["Distribution_ID"] + "', 'inline');");
                    //hplTitle.Attributes.Add("onmouseout", "setVisibility(event,'" + currow["Distribution_ID"] + "', 'none');");
                    //hplTitle.Attributes.Add("class", "chuyenmuc_tomtattindau");
                    hplTitle.Text = ConvertUtility.ToString(currow["Content_Headline"]);
                    hplTitle.NavigateUrl = BuildUrlByDistId((int)currow["Distribution_ID"], this._zoneid);
                    //Image img = (Image)e.Item.FindControl("foter_img");
                    //if (!string.IsNullOrEmpty(currow["Content_Avatar"].ToString()))
                    //{
                    //    img.ImageUrl = MultimediaUtility.GetListAvatar(currow["Content_Avatar"].ToString());
                    //}
                }
            }
        }

        protected void rpt_head_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            DataRowView row = (DataRowView)e.Item.DataItem;
            if (row != null)
            {
                int distid = ConvertUtility.ToInt32(row["Distribution_ID"]);

                Image img = (Image)e.Item.FindControl("img_img");
                if (row["Content_Avatar"] != DBNull.Value && !string.IsNullOrEmpty(row["Content_Avatar"].ToString()) && System.IO.File.Exists(Server.MapPath(row["Content_Avatar"].ToString())))
                {
                    img.ImageUrl = MultimediaUtility.GetRealPath(row["Content_Avatar"].ToString());
                    img.Attributes.Add("onclick", "javascript:window.location='" + BuildUrlByDistId(distid, this._zoneid) + "'");
                    img.Attributes.Add("style", "cursor:hand");
                }
                else
                {
                    //img.ImageUrl = "/images/p1.jpg";
                    img.Visible = false;
                }

                string _url = BuildUrlByDistId((int)row["Distribution_ID"], this._zoneid);

                HyperLink link1 = (HyperLink)e.Item.FindControl("link1");

                link1.Text = ConvertUtility.ToString(row["Content_Headline"]);
                //link1.Text = NCS.Library.Utilities.ConvertUtility.ToStringWithoutFontBold(link1.Text, NCS.Module.CMS.Library.Definitions.Constants.LengthLeftTeaser);
                //link1.NavigateUrl = BuildUrlByDistId((int)row["Distribution_ID"], this._zoneid);
                link1.NavigateUrl = _url;

                Literal lblDate = (Literal)e.Item.FindControl("lblDate");
                lblDate.Text = ConvertUtility.ToDateTime(row["Distribution_CreateDate"]).ToString("dd/MM/yyyy");
                Literal lblTime = (Literal)e.Item.FindControl("lblTime");
                lblTime.Text = ConvertUtility.ToDateTime(row["Distribution_CreateDate"]).ToString("hh:mm:ss tt");
            }
        }
    }
}